#!/bin/bash

if [ -n "$1" ]; then
data_date=$1
else
  data_date=$(date -d "-1 day" +%F)
fi

HIVE_SQL="
create function default.ip_to_region as 'jtp.ads.udf.ParseIpToRegion'
    using jar 'hdfs://node101:8020/warehouse/app_jars/jtp-ads-warehouse-1.0-SNAPSHOT.jar';


create function default.ua_to_browser as 'jtp.ads.udf.ParseUaToBrowser'
    using jar 'hdfs://node101:8020//warehouse/app_jars/jtp-ads-warehouse-1.0-SNAPSHOT.jar';
"

TMP_DWD_ADS_EVENT_LOG_REGION_SQL="
create table if not exists jtp_ads_warehouse.tmp_dwd_ads_event_log_region
as
select
    event_time,
    event_type,
    ads_id,
    ad_name,
    ads_product_id,
    ads_product_name,
    ads_product_price,
    ads_material_id,
    ads_group_id,
    platform_id,
    platform_name_en,
    platform_name_zh,

    region_map['country'] AS client_country,
    region_map['area' ] AS client_area,
    region_map['province'] AS client_province,
    region_map['city'] AS client_city ,
    client_ip,
    client_device_id,
    client_os_type,
    client_user_agent
    from (
        select
            *,
            default.ip_to_region(client_ip) as region_map
            from jtp_ads_warehouse.tmp_dwd_ads_event_log_dim
    )t1;
"
TMP_DWD_ADS_EVENT_LOG_BROWSER_SQL="
create table if not exists jtp_ads_warehouse.tmp_dwd_ads_event_log_browser
as
select

    event_type,
    ads_id,
    ad_name,
    ads_product_id,
    ads_product_name,
    ads_product_price,
    ads_material_id,
    ads_group_id,
    platform_id,
    platform_name_en,
    platform_name_zh,
    client_country,
    client_area,
    client_province,
    client_city,
    client_ip,
    client_device_id,
    client_os_type,

    browser_map['os_version'] as client_os_version,
    browser_map['browser'] as client_browser_type,
    browser_map['browser_version'] as client_browser_version,
    client_user_agent
from
(
    select
        *,
        default.ua_to_browser(client_user_agent) as browser_map
    from jtp_ads_warehouse.tmp_dwd_ads_event_log_region
)t1;
"
/opt/module/hive/bin/beeline -u jdbc:hive2://node101:10000 -n bwie -e "${HIVE_SQL}"
/opt/module/spark/bin/beeline -u jdbc:hive2://node101:10001 -n bwie -e "${TMP_DWD_ADS_EVENT_LOG_REGION_SQL}${TMP_DWD_ADS_EVENT_LOG_BROWSER_SQL}"


